package com.hmy.uaa.controller.api;

import com.hmy.uaa.bo.request.DepartmentRequest;
import com.hmy.uaa.po.Department;
import com.hmy.uaa.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.List;

/**
 *
 */
@Api(tags = "部门接口")
@Validated
@RestController
@RequestMapping("/api/department")
@Slf4j
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @ApiOperation("新增部门")
    @PostMapping
    public void save(@RequestBody @Validated({DepartmentRequest.Insert.class, Default.class}) DepartmentRequest departmentRequest) {

        log.info(departmentRequest.toString());
        departmentService.save(departmentRequest);
    }

    @ApiOperation("编辑部门")
    @PutMapping
    public void update(@RequestBody @Validated({DepartmentRequest.Update.class, Default.class}) DepartmentRequest departmentRequest) {

        log.info(departmentRequest.toString());
        departmentService.update(departmentRequest);
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        departmentService.delete(id);
    }

    @ApiOperation("查询部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "节点id，如果值为0，表示查询所有节点")
    })
    @GetMapping("/{id}")
    public List<Department> queryList(@PathVariable Long id) {

        return departmentService.queryList(id);
    }
}
